package com.pro.common.service.user.fallback;

import com.pro.common.api.model.BaseUserModel;
import com.pro.common.api.user.intf.UserBatchInfoFeignClient;
import com.pro.common.api.user.model.db.User;
import com.pro.common.api.user.model.db.UserBatchInfo;
import com.pro.common.service.model.mapstruct.UserModelMapper;
import com.pro.common.service.user.service.UserBatchInfoService;
import com.pro.common.service.user.service.UserService;
import com.pro.common.service.user.service.unit.UserBatchUnitService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Tag(name = "子账号信息")
@RestController
@RequestMapping("/")
@AllArgsConstructor
public class UserBatchInfoFeignFallback implements UserBatchInfoFeignClient {
    private UserService<?, ? extends User> userService;
    private UserBatchUnitService unitService;
    private UserBatchInfoService service;
    private UserModelMapper userModelMapper;


    @Override
    public List<Long> saveBatch(List<UserBatchInfo> list) {
        Map<Long, ? extends User> userMap = userService.idMap(list.stream().map(BaseUserModel::getUserId).toList());
        list.forEach(o -> userModelMapper.copy(userMap.get(o.getUserId()), o));
        unitService.saveBatch(list);
//        service.saveBatch(list);
        return list.stream().map(UserBatchInfo::getId).toList();
    }
}
